math.randomseed(os.time()) -- TODO this is inefficient? It's executed once per object that has 'require "util.util"'?

COMPARETO_FIRST_IS_SMALLER = -1
COMPARETO_FIRST_IS_BIGGER = 1
COMPARETO_FIRST_AND_SECOND_ARE_EQUAL = 0

--- Compare two values, determine which is bigger
-- @param first First value
-- @param second Second value
-- @return COMPARETO_FIRST_IS_SMALLER if the first is smaller, COMPARETO_FIRST_IS_BIGGER if the first is bigger, COMPARETO_FIRST_AND_SECOND_ARE_EQUAL if they are equal
function compareTo(first, second)
    local result = COMPARETO_FIRST_AND_SECOND_ARE_EQUAL
    if first < second then
        result = COMPARETO_FIRST_IS_SMALLER
    end
    if first > second then  
        result = COMPARETO_FIRST_IS_BIGGER;
    end
    return result
end

--- Split a string based on a certain delimiter
-- @param str String to split
-- @param delim Delimiter to split on
-- @param maxNb Maximum number of times to split, or nil
-- @return Table containing split string
function Split(str, delim, maxNb)
    -- Eliminate bad cases...
    if string.find(str, delim) == nil then
        return { str }
    end
    if maxNb == nil or maxNb < 1 then
        maxNb = 0    -- No limit
    end
    local result = {}
    local pat = "(.-)" .. delim .. "()"
    local nb = 0
    local lastPos
    for part, pos in string.gfind(str, pat) do
        nb = nb + 1
        result[nb] = part
        lastPos = pos
        if nb == maxNb then break end
    end
    -- Handle the last field
    if nb ~= maxNb then
        result[nb + 1] = string.sub(str, lastPos)
    end
    return result
end

--- Check if a table contains a certain object
-- @param list Table to search through
-- @param candidate Object to look for
-- @return boolean
function contains(list, candidate)
	for _,v in pairs(list) do
		if v == candidate then	
			return true
		end
	end
    	return false
end

--- Determine the size of of a table
-- @param table Table to get the size of
-- return Integer Number of elements in the table
function size(list)
	if type(list)~="table" then
		error("argument '"..list.."' is not a table")
	end
	local count = 0
	for _,_ in pairs(list) do
		count = count+1
	end
	return count
end

--- Return values based on a boolean value
-- @param condition Boolean value to compare
-- @param a Value to return if condition is true
-- @param b Value to return if condition is false
-- @return a or b depending on condition
function iif(condition,a,b)
        if condition then
                return a
        else
                return b
        end
end

--- Determine if a string ends with a specific value
-- @param stringToSearchIn String to look for the value in
-- @param stringToSearchFor String to search for.  This can optionally be a table of values to look for
-- @return boolean
function endswith(stringToSearchIn, stringToSearchFor)
    local result = false
    if (stringToSearchIn ~= nil and stringToSearchFor ~= nil) then
        if type(stringToSearchFor) == "table" then
            for key,value in ipairs(stringToSearchFor) do
                if string.sub(stringToSearchIn, string.len(stringToSearchIn) - string.len(value) + 1) == value then 
                    result = true
                    break
                end
            end
        else 
            result = string.sub(stringToSearchIn, string.len(stringToSearchIn) - string.len(stringToSearchFor) + 1) == stringToSearchFor
        end
    end
    return result
end

function errorReportsMessage(error, message)
    return endswith(error, message)
end

function getRandomFrom(candidates)
    return candidates[math.random(1, #candidates)]
end
